package com.xtoad.ecms.common.utils.generate;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public interface IGenerator {
    /**
     * 根据包路径和类名生成类
     *
     * @param packagePath 包路径
     * @param modelName   类名
     * @return 生成的类
     */
    String generate(String packagePath, String modelName);

    /**
     * 批量生成
     *
     * @param packagePath 包路径
     * @param classNames  类名
     * @param isOverwrite 是否强制生成
     * @return 生成的类
     */
    default Map<String, String> batchGenerate(String packagePath, Set<String> classNames, boolean isOverwrite) {
        System.out.println("batch generate start ");

        Map<String, String> fileMap = new HashMap<>(6);
        List<String> existClassList = Generator.getClassNameNoPackage(packagePath, false);
        classNames.forEach(className -> {
            String[] split = className.split("\\.");
            String modelName = split[split.length - 1];
            String thisName = MessageFormat.format("I{0}Repository", modelName);
            if (existClassList.contains(thisName) && !isOverwrite) {
                return;
            }
            String content = generate(packagePath, modelName);
            String key = packagePath + "." + thisName;
            fileMap.put(key, content);
        });
        System.out.println("batch generate end ");
        return fileMap;
    }
}
